// pages/Home/home/home.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    aliyunServrBaseURL: getApp().config.aliyunServrBaseURL,//阿里云公共地址
    s: app.system.statusBarHeight, //状态栏高度
    n: (app.menu.top - app.system.statusBarHeight) * 2 + app.menu.height, //导航栏高度
    h: app.menu.height, //胶囊高度
    // '找产品', '找厂家',
    tabs: ['货行行集采购'],
    activeIndex: 2,//选择的下标
    banner: [],//轮播图
    value: 3,//评分星际
    page: 1,//页码
    pagingState: false,//分页状态
    goodsList: [],//商品
    overlayShow: false,//后台控制
    num: '',//购物车数量
    pageMill: 1,//页码找厂家
    pagingStateMill: false,//分页状态  找厂家
    shopListMill: [],//厂家
    progressPer: 0,//滚动条距离
    pagePurchase: 1,//页码集采购
    pagingStatePurchase: false,//分页状态  集采购
    purchaseGoodsList: [],//集采购
    BubbleList: [],
    current: 0,
    style_type: 0,
    config: {}
  },
  swiperChange(event) {
    let { current } = event.detail
    this.setData({ current })
  },
  async getBubble() {
    const { code, data } = await getApp().api.getBubble()
    if (code == 1) {
      this.setData({ BubbleList: data.lists })
    }
  },
  /**找厂家 */
  async getShopList() {
    wx.stopPullDownRefresh();
    let object = {
      page_no: this.data.pageMill,
      page_size: 5
    }
    if (!this.data.pagingStateMill) {
      const { code, data } = await getApp().api.getShopList(object)
      if (code == 1) {
        this.setData({
          shopListMill: this.data.shopListMill.concat(data.list)
        })
        if (data.more != 1) {
          this.setData({
            pagingStateMill: true
          })
          return
        }
        this.setData({
          pageMill: (this.data.pageMill * 1) + 1
        })
        console.log(this.data.pageMill);
      }
    }
  },
  /**获取购物车数量 */
  async cartnum() {
    const { code, data } = await getApp().api.cartnum()
    if (code == 1) {
      this.setData({
        num: data.num
      })
    }
  },
  /**金刚去 */
  async menulists() {
    let object = {
      module_type: this.data.activeIndex
    }
    const { code, data } = await getApp().api.menulists(object)
    if (code == 1) {
      this.setData({
        menulist: data.data,
        style_type: data.style_type
      })
    }
  },
  /**关闭弹窗广告 */
  onClickHide() {
    this.data.config.is_show_popup = !this.data.config.is_show_popup
    this.setData({ config: this.data.config });
    wx.navigateTo({
      url: this.data.config.popup_link,
    })
  },
  /**商品列表 */
  async getGoodsList() {
    wx.stopPullDownRefresh();
    let object = {
      page_no: this.data.page
    }
    if (!this.data.pagingState) {
      const { code, data } = await getApp().api.getGoodsList(object)
      if (code == 1) {
        this.setData({
          goodsList: this.data.goodsList.concat(data.lists)
        })
        if (data.more != 1) {
          this.setData({
            pagingState: true
          })
          return
        }
        this.setData({
          page: (this.data.page * 1) + 1
        })
      }
    }
  },
  /**集采购列表 */
  async getpdGoodsList() {
    wx.stopPullDownRefresh();
    let object = {
      page_no: this.data.pagePurchase
    }
    if (!this.data.pagingStatePurchase) {
      const { code, data } = await getApp().api.getpdGoodsList(object)
      if (code == 1) {

        this.setData({
          purchaseGoodsList: this.data.purchaseGoodsList.concat(data.lists)
        })
        if (data.more != 1) {
          this.setData({
            pagingStatePurchase: true
          })
          return
        }
        this.setData({
          pagePurchase: (this.data.pagePurchase * 1) + 1
        })
      }
    }
  },
  /** 跳转 */
  navigateTo(event) {
    let url = event.currentTarget.dataset.url
    console.log(url);
    wx.navigateTo({
      url,
    })
  },
  /**
  * tabs切换
  * @param {*} e 
  */
  onTabTap(e) {
    const index = e.currentTarget.dataset.index;
    this.setData({
      activeIndex: index,
    });
  },
  onChange(event) {
    this.setData({
      activeIndex: event.detail.index
    })
    this.menulists()
    if (event.detail.index == 0) {
      this.getPositionList('pbanner')//广告
    } else if (event.detail.index == 1) {
      this.getPositionList('cbanner')//广告
    } else if (event.detail.index == 2) {
      this.getPositionList('gbanner')//广告
    }

  },
  /**广告位 */
  async getPositionList(ad_sn) {
    let object = {
      ad_sn
    }
    const { code, data } = await getApp().api.getPositionList(object)
    if (code == 1) {
      this.setData({
        banner: data
      })
    }
  },
  /**配置项 */
  async config() {
    const { code, data } = await getApp().api.config()
    if (code == 1) {
      getApp().local.set('config', data)
      this.setData({
        config: data
      })
    }
  },
  scrollBarChange(event) {
    let progressPer = 0
    let {
      scrollLeft,
      scrollWidth
    } = event.detail
    progressPer = (scrollLeft / (scrollWidth - 370)) * 100
    progressPer = Number(progressPer.toFixed(0))
    this.setData({
      progressPer
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.config()//配置
    this.getPositionList('pbanner')//广告
    this.getGoodsList()//商品列表
    this.menulists()//金刚区
    this.getShopList()//找厂家
    this.getpdGoodsList()//及采购
    this.getBubble()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (getApp().local.get('token')) {
      this.cartnum()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    let index = this.data.activeIndex
    switch (index) {
      case 0:
        console.log('产品');
        this.setData({
          page: 1,//页码
          pagingState: false,//分页状态
          goodsList: [],//商品
        })
        this.getGoodsList()
        break;
      case 1:
        this.setData({
          pageMill: 1,//页码找厂家
          pagingStateMill: false,//分页状态  找厂家
          shopListMill: [],//厂家
        })
        console.log('厂家');
        this.getShopList()
        break;
      case 2:
        this.setData({
          pagePurchase: 1,//页码集采购
          pagingStatePurchase: false,//分页状态  集采购
          purchaseGoodsList: [],//集采购
        })
        this.getpdGoodsList()
        console.log('及采购');
        break;
      default:
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let index = this.data.activeIndex
    switch (index) {
      case 0:
        console.log('产品');
        this.getGoodsList()
        break;
      case 1:
        console.log('厂家');
        this.getShopList()
        break;
      case 2:
        this.getpdGoodsList()
        console.log('及采购');
        break;
      default:
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})